package com.egoo.ticket.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.dao.*;
import com.egoo.ticket.server.pojo.dto.ConditionOutputDto;
import com.egoo.ticket.server.pojo.dto.PageOutputDto;
import com.egoo.ticket.server.pojo.dto.TableHeadFieldDto;
import com.egoo.ticket.server.pojo.dto.WorkOrderFilterMenuInputDto;
import com.egoo.ticket.server.pojo.entity.EwsUser;
import com.egoo.ticket.server.pojo.entity.FilterFirst;
import com.egoo.ticket.server.pojo.entity.FilterSecond;
import com.egoo.ticket.server.pojo.entity.WorkOrderFilterMenu;
import com.egoo.ticket.server.pojo.vo.*;
import com.egoo.ticket.server.service.HeadFieldService;
import com.egoo.ticket.server.service.WorkOrderFilterMenuService;
import com.egoo.ticket.server.service.WorkOrderTypeMenuService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.DynamicSqlJoinUtil;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class WorkOrderFilterMenuServiceImpl implements WorkOrderFilterMenuService {

	@Autowired
	private WorkOrderFilterMenuDao workOrderFilterMenuDao;
	@Autowired
	private EwsUserDao ewsUserDao;
	@Autowired
	private EwsRoleDao ewsRoleDao;
	@Autowired
	private FilterFirstDao filterFirstDao;
	@Autowired
	private FilterSecondDao filterSecondDao;
	@Autowired
	private EwsUserRoleDao ewsUserRoleDao;
	@Autowired
	private WorkOrderTypeMenuService workOrderTypeMenuService;
	@Autowired
	private DynamicSqlJoinUtil dynamicSqlJoinUtil;
	@Autowired
	HeadFieldService tableHeadFieldService;


	/**
	 * 查询所有的过滤器菜单信息分页
	 * 
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageOutputDto selectAll(Integer currentPage, Integer pageSize, String tenantId) {
		PageOutputDto pageOutputDto = new PageOutputDto();
		List<WorkOrderFilterMenu> workOrderFilterMenuList = workOrderFilterMenuDao
				.selectAll((currentPage - 1) * pageSize, pageSize,tenantId);
		Integer total = workOrderFilterMenuDao.selectTotal(tenantId);
		pageOutputDto.setList(workOrderFilterMenuList);
		pageOutputDto.setTotal(total);
		log.info("selectAll: {}", pageOutputDto);
		return pageOutputDto;
	}

	/**
	 * 根据Id查询过滤器菜单信息
	 *
	 * @return
	 */
	@Override
	public WorkOrderFilterMenu selectById(Integer id,String tenantId) {
		WorkOrderFilterMenu workOrderFilterMenu = workOrderFilterMenuDao.selectById(id,tenantId);
		TableHeadFieldDto tableHeadFieldDto = new TableHeadFieldDto();
		tableHeadFieldDto.setWorkOrderFilterMenuId(workOrderFilterMenu.getId().longValue());
		ArrayList<TableHeadFieldVo> tableHeadFieldVos = tableHeadFieldService.select(tableHeadFieldDto);
		workOrderFilterMenu.setFieldInfoList(tableHeadFieldVos);
		return workOrderFilterMenu;
	}

	/**
	 * TODO 根据菜单名称和工单类型获取过滤器菜单信息
	 * @author: victor_tang
	 * @createtime: 2020/3/3 17:29
	 * @param name 1
	 * @param workOrderTypeId 2
	 * @param currentPage 3
	 * @param pageSize 4
	 * @param tenantId 5
	 * @return: com.egoo.ews.service.dto.PageOutputDto
	 */
	@Override
	public DbResponse selectByNameAndOrderTypeName(String name, String workOrderTypeId,
												   Integer currentPage, Integer pageSize, String tenantId) {
		// 设置分页参数
		PageHelper.startPage(currentPage,pageSize);
		Map<String, Object> searchMap = new HashMap<>(8);
		searchMap.put("name",name);
		searchMap.put("tenantId",tenantId);
		if (!StringUtils.isEmpty(workOrderTypeId) && !ConstantCode.UserRoleDeptType.ALL.equals(workOrderTypeId)) {
			searchMap.put("workOrderTypeId",Long.valueOf(workOrderTypeId));
		}
		List<WorkOrderFilterMenu> workOrderFilterMenuList =
				workOrderFilterMenuDao.selectFilterMenuListByCondition(searchMap);
		// 处理可见范围名称
		for (WorkOrderFilterMenu workOrderFilterMenu : workOrderFilterMenuList) {
			String filterMenuVisibleNames = workOrderTypeMenuService.translateMenuVisibleName(workOrderFilterMenu.getFilterMenuVisibleType(),
					workOrderFilterMenu.getFilterMenuVisibleIds(),tenantId);

			workOrderFilterMenu.setFilterMenuVisibleNames(filterMenuVisibleNames);
		}
		PageInfo<WorkOrderFilterMenu> workOrderFilterMenuPageInfo = new PageInfo<>(workOrderFilterMenuList);

		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,
				ResponseConstant.SELECT_SUCCESS_MSG,workOrderFilterMenuList,
				workOrderFilterMenuPageInfo.getTotal());
	}

	/**
	 * 返回当前排序最大值+1
	 *
	 * @return
	 */
	@Override
	public Integer selectOrderMax(String tenantId) {
		Integer count = 0;
		List<WorkOrderFilterMenu> workOrderFilterMenuList = workOrderFilterMenuDao.selectAllMenu(tenantId);
		if (null != workOrderFilterMenuList && !workOrderFilterMenuList.isEmpty()) {
			count = workOrderFilterMenuDao.selectOrderMax() + 1;
			return count;
		}
		count = count + 1;
		return count;
	}

	/**
	 * 根据过滤器菜单的排序获取过滤器菜单信息
	 * 
	 * @param ordering
	 * @return
	 */
	@Override
	public WorkOrderFilterMenu selectByOrdering(Integer ordering,String tenantId) {
		return workOrderFilterMenuDao.selectByOrdering(ordering,tenantId);
	}

	/**
	 *
	 * 根据过滤器菜单名称和工单类型Id获取过滤器菜单信息
	 * 
	 * @param name
	 * @param workOrderTypeId
	 * @return
	 */
	@Override
	public WorkOrderFilterMenu selectByEqualsName(String name, Long workOrderTypeId,String tenantId) {
		return workOrderFilterMenuDao.selectByEqualsName(name, workOrderTypeId,tenantId);
	}

	/**
	 * 新建过滤器菜单
	 *
	 * @param workOrderFilterMenuInputDto
	 * @return
	 */
	@Override
	public Integer insertMenu(WorkOrderFilterMenuInputDto workOrderFilterMenuInputDto) {
		WorkOrderFilterMenu workOrderFilterMenu = new WorkOrderFilterMenu();
		VisibleRangeVo visibleRange = workOrderFilterMenuInputDto.getVisibleRange();
		VisibleRangeVo dataRange = workOrderFilterMenuInputDto.getDataRange();
		workOrderFilterMenu.setFilterMenuVisibleType(visibleRange.getType());
		workOrderFilterMenu.setFilterMenuDataType(dataRange.getType());
		String filterMenuVisibleIds = null;
		String filterMenuDataTypeIds = null;
		if (ConstantCode.UserRoleDeptType.DEPT.equals(visibleRange.getType())) {
			filterMenuVisibleIds = visibleRange.getValue().toJSONString();
			filterMenuDataTypeIds = dataRange.getValue().toJSONString();
		}else{
			filterMenuVisibleIds = StringUtils.join(visibleRange.getValue(), ",");
			filterMenuDataTypeIds = StringUtils.join(dataRange.getValue(),",");
		}
		workOrderFilterMenu.setFilterMenuVisibleIds(filterMenuVisibleIds);
		workOrderFilterMenu.setFilterMenuDataTypeIds(filterMenuDataTypeIds);
//		JSONObject visibleRange1 = JSONObject.parseObject(JSON.toJSONString(visibleRange));
//		WorkOrderFilterMenu workOrderFilterMenu = new WorkOrderFilterMenu();
//		String nameList = "";
//		if (visibleRange1.getString("type").equals(ConstantCode.UserRoleDeptType.ALL)) {
//			workOrderFilterMenu.setVisibleRange(visibleRange1);
//		} else if (visibleRange1.getString("type").equals(ConstantCode.UserRoleDeptType.USER)) {
//			for (int i = 0; i < visibleRange1.getJSONArray("value").size(); i++) {
//				EwsUser ewsUser = ewsUserDao.selectById((visibleRange1.getJSONArray("value").getInteger(i)).toString());
//				if (i == 0) {
//					nameList += ewsUser.getName();
//				} else {
//					nameList += "," + ewsUser.getName();
//				}
//
//			}
//			visibleRange.setNameList("" + nameList);
//			JSONObject visibleRange2 = JSONObject.parseObject(JSON.toJSONString(visibleRange));
//			workOrderFilterMenu.setVisibleRange(visibleRange2);
//		} else {
//			for (int i = 0; i < visibleRange1.getJSONArray("value").size(); i++) {
//				EwsRole ewsRole = ewsRoleDao.selectByRoleId((visibleRange1.getJSONArray("value").getInteger(i).toString()));
//				if (i == 0) {
//					nameList += ewsRole.getName();
//				} else {
//					nameList += "," + ewsRole.getName();
//				}
//			}
//			visibleRange.setNameList("" + nameList);
//			JSONObject visibleRange2 = JSONObject.parseObject(JSON.toJSONString(visibleRange));
//			workOrderFilterMenu.setVisibleRange(visibleRange2);
//		}
		/**
		 * 新建过滤器菜单时对筛选条件的处理
		 */
		if (null != workOrderFilterMenuInputDto.getFilterCondition()) {
			FilterConditionVo filterCondition = workOrderFilterMenuInputDto.getFilterCondition();
			List<ConditionVo> conditionList = filterCondition.getCondition();
			JSONArray jsonArray = handleFilterCondition(conditionList);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("condition", jsonArray);
			workOrderFilterMenu.setFilterCondition(jsonObject);

		} else {
			workOrderFilterMenu.setFilterCondition(
					JSONObject.parseObject(JSON.toJSONString(workOrderFilterMenuInputDto.getFilterCondition())));
		}
		String userId = workOrderFilterMenuInputDto.getUserId();
		// 获取当前用户信息
		EwsUser ewsUser = ewsUserDao.selectById(userId);
		String ewsUserName = ewsUser.getName();

		workOrderFilterMenu.setName(workOrderFilterMenuInputDto.getName());
		workOrderFilterMenu.setMenuDesc(workOrderFilterMenuInputDto.getMenuDesc());
		workOrderFilterMenu.setOrdering(workOrderFilterMenuInputDto.getOrdering());
		workOrderFilterMenu.setStatus(workOrderFilterMenuInputDto.getStatus());
		workOrderFilterMenu.setTagId(workOrderFilterMenuInputDto.getTagId());
		workOrderFilterMenu.setTagName(workOrderFilterMenuInputDto.getTagName());
		workOrderFilterMenu.setWorkOrderTypeId(workOrderFilterMenuInputDto.getWorkOrderTypeId());
		workOrderFilterMenu.setWorkOrderTypeName(workOrderFilterMenuInputDto.getWorkOrderTypeName());
		workOrderFilterMenu.setLastUserId(userId);
		Timestamp time = new Timestamp(System.currentTimeMillis());
		workOrderFilterMenu.setLastUpdateTime(time);

		workOrderFilterMenu.setCreateTime(time);
		workOrderFilterMenu.setCreateUserId(userId);
		workOrderFilterMenu.setCreateUserName(ewsUserName);
		workOrderFilterMenu.setLastUserName(ewsUserName);
		workOrderFilterMenu.setTenantId(workOrderFilterMenuInputDto.getTenantId());
		workOrderFilterMenu.setId(((Long)time.getTime()).intValue());
		Integer result;
		//创建表头字段
//		try {
//			workOrderFilterMenu.setId(1000);
			result = workOrderFilterMenuDao.insertMenu(workOrderFilterMenu);
			createTableHead(workOrderFilterMenuInputDto,workOrderFilterMenu);
//		} catch (Exception e) {
//			throw new RuntimeException();
//		}
		return result;
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 创建表头字段信息
	 * @Param:
	 * @return:
	 * @Date: 2020/6/10
	 */
	private void createTableHead(WorkOrderFilterMenuInputDto workOrderFilterMenuInputDto, WorkOrderFilterMenu workOrderFilterMenu) {

		try {
			TableHeadFieldDto tableHeadFieldDto = new TableHeadFieldDto();
			tableHeadFieldDto.setFieldInfoList(workOrderFilterMenuInputDto.getFieldInfoList());
			tableHeadFieldDto.setTenantId(workOrderFilterMenuInputDto.getTenantId());
			tableHeadFieldDto.setWorkOrderFilterMenuId(workOrderFilterMenu.getId().longValue());
			tableHeadFieldService.insert(tableHeadFieldDto);
		} catch (Exception e) {
			log.info("表头字段添加失败"+e);
			throw new RuntimeException();
		}
	}

	/**
	 * TODO 处理过滤器条件信息
	 * @author: victor_tang
	 * @createtime: 2020/1/15 16:21
	 * @param conditionList 1
	 * @return: com.alibaba.fastjson.JSONArray
	 */
	@Override
	public JSONArray handleFilterCondition(List<ConditionVo> conditionList){
		List<ConditionOutputDto> conditionOutputDtoList = new ArrayList<>();
		for (int i = 0; i < conditionList.size(); i++) {
			ConditionOutputDto conditionOutputDto = new ConditionOutputDto();
			// 创建firstId的label,value对象
			InteractiveDataVo interactiveDataFirstId = new InteractiveDataVo();
			// 筛选条件对象
			ConditionVo condition = conditionList.get(i);
			// 筛选条件里的第一部分
			Integer firstId = condition.getFirstId();
			FilterFirst filterFirst = filterFirstDao.selectById(firstId);
			interactiveDataFirstId.setLabel(filterFirst.getName());
			interactiveDataFirstId.setValue(firstId);
			conditionOutputDto.setFirstId(interactiveDataFirstId);
			conditionOutputDto.setFirstIdList(condition.getFirstList());
			conditionOutputDto.setSecondList(condition.getSecondList());
			conditionOutputDto.setThirdList(condition.getThirdList());
			conditionOutputDto.setFourthList(condition.getFourthList());
			// 筛选条件的第二部分
			InteractiveDataVo interactiveDataSecondId = new InteractiveDataVo();
			FilterSecond filterSecond = filterSecondDao.selectContentByFirstId(firstId);
			JSONObject jsonObject = filterSecond.getContent();
			// 是否有typeId也就是用户或者角色
			// 9为工单发起人，10为工单当前受理人，11为工单历史受理人
			if (9 == firstId || 10 == firstId || 11 == firstId) {
				String typeId = condition.getTypeId();
				String name=null;
				for (int j = 0; j <jsonObject.getJSONArray("type").size() ; j++) {
					String type = jsonObject.getJSONArray("type").getJSONObject(j).getString(typeId);
					if (typeId.equals(type)){
						 name = jsonObject.getJSONArray("type").getJSONObject(i).getString("name");
					}
				}

				InteractiveDataVo interactiveDataTypeId = new InteractiveDataVo();
				interactiveDataTypeId.setLabel(name);
				interactiveDataTypeId.setValue(typeId);
				conditionOutputDto.setTypeId(interactiveDataTypeId);
				Integer secondDataId = condition.getSecondDataId();
				String dataName = jsonObject.getJSONArray("data").getJSONObject((secondDataId - 1))
						.getString("name");
				interactiveDataSecondId.setLabel(dataName);
				interactiveDataSecondId.setValue(secondDataId);
				conditionOutputDto.setSecondDataId(interactiveDataSecondId);
				// 创建FilterValue的实体对象
				Object filterValue = condition.getValue();
				conditionOutputDto.setValue(filterValue);
				conditionOutputDto.setTag(2);
				conditionOutputDto.setShapeTag(2);
				if (1 == secondDataId || 2 == secondDataId) {
					conditionOutputDto.setTagIn(1);
				} else {
					conditionOutputDto.setTagIn(3);
				}
			} // 1-4 工单标题，工单描述，客户名称
			else {
				InteractiveDataVo interactiveDataTypeId = new InteractiveDataVo();
				// typeId传入空值
				interactiveDataTypeId.setValue(111);
				interactiveDataTypeId.setLabel(111);
				conditionOutputDto.setTypeId(interactiveDataTypeId);
				Integer secondDataId = condition.getSecondDataId();
				String dataName = jsonObject.getJSONArray("data").getJSONObject((secondDataId - 1))
						.getString("name");
				interactiveDataSecondId.setLabel(dataName);
				interactiveDataSecondId.setValue(secondDataId);
				conditionOutputDto.setSecondDataId(interactiveDataSecondId);
				Object filterValue = condition.getValue();
				conditionOutputDto.setValue(filterValue);
				if (1 == firstId || 2 == firstId || 3 == firstId || 4 == firstId) {
					conditionOutputDto.setShapeTag(1);
					conditionOutputDto.setTag(1);
					conditionOutputDto.setTagIn(0);
				} else if (5 == firstId || 6 == firstId || 7 == firstId || 8 == firstId || 17 == firstId
						|| 18 == firstId || 20 ==firstId) {
					conditionOutputDto.setShapeTag(2);
					conditionOutputDto.setTag(1);
					if (1 == secondDataId || 2 == secondDataId) {
						conditionOutputDto.setTagIn(1);
					} else {
						conditionOutputDto.setTagIn(3);
					}
				} else if (12 == firstId || 13 == firstId) {
					conditionOutputDto.setShapeTag(3);
					conditionOutputDto.setTag(1);
					if (1 == secondDataId || 2 == secondDataId) {
						conditionOutputDto.setTagIn(1);
					} else {
						conditionOutputDto.setTagIn(2);
					}
				} else if (14 == firstId) {
					conditionOutputDto.setShapeTag(4);
					conditionOutputDto.setTag(1);
					conditionOutputDto.setTagIn(1);
				} else {
					conditionOutputDto.setShapeTag(5);
					if (1 == secondDataId || 2 == secondDataId) {
						conditionOutputDto.setTagIn(1);
					} else {
						conditionOutputDto.setTagIn(2);
					}
				}
			}

			conditionOutputDtoList.add(conditionOutputDto);

		}

		return JSONArray.parseArray(JSON.toJSONString(conditionOutputDtoList));
	}

	/**
	 * 批量创建过滤器菜单
	 *
	 * @param list
	 * @return
	 */
	@Override
	public Integer insertByBatch(List<WorkOrderFilterMenu> list) {
		Integer result = workOrderFilterMenuDao.insertByBatch(list);
		return result;
	}

	/**
	 * 根据id删除过滤器菜单
	 *
	 * @param id
	 * @return
	 */
	@Override
	public Integer deleteMenu(Integer id,String tenantId) {
		WorkOrderFilterMenu workOrderFilterMenu = workOrderFilterMenuDao.selectById(id,tenantId);
		workOrderFilterMenu.setTenantId(tenantId);
		return workOrderFilterMenuDao.deleteMenu(workOrderFilterMenu);
	}

	/**
	 * 更新过滤器菜单的使用状态
	 *
	 * @param id
	 * @param userId
	 * @param status
	 * @return
	 */
	@Override
	public Integer updateUseStatus(Integer id, String userId, Integer status,String tenantId) {
		WorkOrderFilterMenu workOrderFilterMenu = workOrderFilterMenuDao.selectById(id,tenantId);
		EwsUser ewsUser = ewsUserDao.selectById(userId);
		workOrderFilterMenu.setLastUserId(userId);
		workOrderFilterMenu.setLastUserName(ewsUser.getName());
		Timestamp time = new Timestamp(System.currentTimeMillis());
		workOrderFilterMenu.setLastUpdateTime(time);
		workOrderFilterMenu.setStatus(status);
		return workOrderFilterMenuDao.updateUseStatus(workOrderFilterMenu);
	}

	/**
	 * 编辑过滤器菜单
	 * 
	 * @param workOrderFilterMenuInputDto
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public Integer updateContent(WorkOrderFilterMenuInputDto workOrderFilterMenuInputDto) {
		Integer id = workOrderFilterMenuInputDto.getId();
		String tenantId = workOrderFilterMenuInputDto.getTenantId();
		WorkOrderFilterMenu workOrderFilterMenu = workOrderFilterMenuDao.selectById(id,tenantId);
		VisibleRangeVo visibleRange = workOrderFilterMenuInputDto.getVisibleRange();
		VisibleRangeVo dataRange = workOrderFilterMenuInputDto.getDataRange();
		workOrderFilterMenu.setFilterMenuVisibleType(visibleRange.getType());
		workOrderFilterMenu.setFilterMenuDataType(dataRange.getType());
		String filterMenuVisibleIds = null;
		String filterMenuDataTypeIds = null;
		if (ConstantCode.UserRoleDeptType.DEPT.equals(visibleRange.getType())) {
			filterMenuVisibleIds = visibleRange.getValue().toJSONString();

		}else{
			filterMenuVisibleIds = StringUtils.join(visibleRange.getValue(), ",");
		}
		if (ConstantCode.UserRoleDeptType.DEPT.equals(dataRange.getType())) {

			filterMenuDataTypeIds = dataRange.getValue().toJSONString();
		}else{

			filterMenuDataTypeIds = StringUtils.join(dataRange.getValue(),",");
		}
		workOrderFilterMenu.setFilterMenuVisibleIds(filterMenuVisibleIds);
		workOrderFilterMenu.setFilterMenuDataTypeIds(filterMenuDataTypeIds);

		/**
		 * 更新过滤器菜单时对筛选条件的处理
		 */

		if (null != workOrderFilterMenuInputDto.getFilterCondition()) {
			FilterConditionVo filterCondition = workOrderFilterMenuInputDto.getFilterCondition();
			List<ConditionVo> conditionList = filterCondition.getCondition();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("condition", handleFilterCondition(conditionList));
			workOrderFilterMenu.setFilterCondition(jsonObject);

		} else {
			workOrderFilterMenu.setFilterCondition(
					JSONObject.parseObject(JSON.toJSONString(workOrderFilterMenuInputDto.getFilterCondition())));
		}

		workOrderFilterMenu.setName(workOrderFilterMenuInputDto.getName());
		workOrderFilterMenu.setOrdering(workOrderFilterMenuInputDto.getOrdering());
		workOrderFilterMenu.setMenuDesc(workOrderFilterMenuInputDto.getMenuDesc());
		workOrderFilterMenu.setStatus(workOrderFilterMenuInputDto.getStatus());
		if (id != 1 && id != 2 && id != 3 && id !=4 && id !=5 && id !=6 && id !=7) {
			workOrderFilterMenu.setWorkOrderTypeId(workOrderFilterMenuInputDto.getWorkOrderTypeId());
			workOrderFilterMenu.setWorkOrderTypeName(workOrderFilterMenuInputDto.getWorkOrderTypeName());
		}
		String userId = workOrderFilterMenuInputDto.getUserId();
		// 获取当前用户信息
		EwsUser ewsUser = ewsUserDao.selectById(userId);
		workOrderFilterMenu.setLastUserId(userId);
		workOrderFilterMenu.setLastUserName(ewsUser.getName());
		Timestamp time = new Timestamp(System.currentTimeMillis());
		workOrderFilterMenu.setLastUpdateTime(time);
		workOrderFilterMenu.setTenantId(tenantId);
		Integer integer;
		try {
			if (id !=1 && id !=2 && id !=3 && id !=4 && id !=5 && id !=6 && id !=7) {
				updateTableHead(workOrderFilterMenuInputDto,workOrderFilterMenu);
			}
			integer = workOrderFilterMenuDao.updateContent(workOrderFilterMenu);

		} catch (Exception e) {
			throw new RuntimeException();
		}
		return integer;
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 更新表头字段
	 * @Param:
	 * @return:
	 * @Date: 2020/6/10
	 */
	private void updateTableHead(WorkOrderFilterMenuInputDto workOrderFilterMenuInputDto, WorkOrderFilterMenu workOrderFilterMenu) {
		try {
			TableHeadFieldDto tableHeadFieldDto = new TableHeadFieldDto();
			tableHeadFieldDto.setWorkOrderFilterMenuId(workOrderFilterMenu.getId().longValue());
			tableHeadFieldDto.setTenantId(workOrderFilterMenuInputDto.getTenantId());
			tableHeadFieldDto.setFieldInfoList(workOrderFilterMenuInputDto.getFieldInfoList());
			Integer update = tableHeadFieldService.update(tableHeadFieldDto);
		} catch (Exception e) {
			log.info("表头信息更新失败"+e);
			throw new RuntimeException();
		}
	}

	/**
	 * 批量更新过滤器菜单所属工单类型
	 * 
	 * @param userId
	 * @param workOrderTypeIdSrc 原工单类型Id
	 * @param workOrderTypeName  修改后的工单类型名字
	 * @return
	 */
	@Override
	public DbResponse updateWorkOrderTypeByBatch(String userId,
												 Long workOrderTypeIdSrc, String workOrderTypeName) {
		List<WorkOrderFilterMenu> workOrderFilterMenuList = workOrderFilterMenuDao
				.selectByWorkOrderTypeId(workOrderTypeIdSrc);
		EwsUser ewsUser = ewsUserDao.selectById(userId);
		for (WorkOrderFilterMenu workOrderFilterMenu : workOrderFilterMenuList) {
			workOrderFilterMenu.setLastUserId(userId);
			workOrderFilterMenu.setLastUserName(ewsUser.getName());
			Timestamp time = new Timestamp(System.currentTimeMillis());
			workOrderFilterMenu.setLastUpdateTime(time);
			workOrderFilterMenu.setWorkOrderTypeName(workOrderTypeName);
			workOrderFilterMenuDao.updateContent(workOrderFilterMenu);
		}
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,
				ResponseConstant.UPDATE_SUCCESS_MSG);
	}

	/**
	 * 根据过滤器id和用户id
	 * 
	 * @param id
	 * @param userId
	 * @return
	 */
	@Override
	public String selectByDynamic(Integer id, String userId,String tenantId) {
		//work_order_relation 工单表一一对应的补充信息表 （查询工单所有信息）
		String sql = "select a.*,"+
				"b.work_order_id,b.urge_count,b.redeploy_count,b.contact_work_order_id_str,b.is_time_out,b.session_id,b.external_work_order_id,b.work_order_monitor_id,b.merge_base_work_order_ids,b.is_recieve,b.sender,b.is_children " +
				"from work_order a,work_order_relation b " +
				"where  a.id = b.work_order_id";

		//添加过滤器所属工单类型条件（workOrderTypeId为0时，代表系统创建的我的工单过滤器，默认查询该租户下所有工单）
		WorkOrderFilterMenu workOrderFilterMenu = workOrderFilterMenuDao.selectById(id,tenantId);
		//获取过滤器处理的流程 先过滤出属于该流程下的工单
		Long workOrderTypeId = workOrderFilterMenu.getWorkOrderTypeId();
		if (workOrderTypeId!=null && workOrderTypeId != 0) {
			sql += " and a.work_order_type_id = '" + workOrderTypeId + "'";
		}
        //获取过滤器表中保存的json filter_condition字段
		JSONObject jsonObject = workOrderFilterMenu.getFilterCondition();
		// 拼接过滤器条件
		sql = dynamicSqlJoinUtil.joinSqlCondition(jsonObject, sql,userId,
				ConstantCode.SqlRelationKey.AND);
		return sql;
	}
}
